package com.chenfan.mcn.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.enums.MessageStatusEnum;
import com.chenfan.mcn.enums.MessageTopicEnum;
import com.chenfan.mcn.model.MessageInfo;
import com.chenfan.mcn.dao.MessageInfoMapper;
import com.chenfan.mcn.service.MessageInfoService;
import com.chenfan.mcn.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 消息信息表 服务实现类
 * </p>
 *
 * @author wulg
 * @since 2021-07-23
 */
@Service
public class MessageInfoServiceImpl implements MessageInfoService {

    @Autowired
    private MessageInfoMapper messageInfoMapper;

    @Override
    public boolean addMessageInfo(MessageInfo messageInfo) {
        messageInfo.setCreateDate(new Date());
        messageInfo.setIsDelete(0);
        return messageInfoMapper.addMessageInfo(messageInfo) > 0;
    }

    @Override
    public boolean idempotentArchivingCode(String billCode, String archivingCode,Long tenantId){
        JSONObject jsonObject = new JSONObject(2);
        jsonObject.put("billCode", billCode);
        jsonObject.put("archivingCode", archivingCode);
        String messageBody = jsonObject.toJSONString();
        String signature = CommonUtil.hmacMd5Hex(messageBody);
        MessageInfo messageInfo = MessageInfo.builder().messageTopic(MessageTopicEnum.ARCHIVING_CODE_IDEMPOTENT.name())
                .messageKey(archivingCode).messageBody(messageBody).signature(signature).tenantId(tenantId)
                .status(MessageStatusEnum.SUCCESS.getType()).build();
        return messageInfoMapper.addMessageInfo(messageInfo) > 0;
    }

    @Override
    public boolean existMessageHeader(String messageTopic, String messageHeader, Long tenantId) {
        return messageInfoMapper.exist(Wrappers.lambdaQuery(MessageInfo.class)
                .eq(MessageInfo::getMessageTopic, messageTopic)
                .eq(MessageInfo::getMessageHeader, messageHeader)
                .eq(MessageInfo::getTenantId, tenantId)
                .eq(MessageInfo::getIsDelete, NumberCst.ZERO));
    }

    @Override
    public MessageInfo getMessageByKey(String messageTopic, String messageKey, String messageHeader, Long tenantId) {
        return messageInfoMapper.selectOne(Wrappers.lambdaQuery(MessageInfo.class)
                .eq(MessageInfo::getMessageTopic, messageTopic)
                .eq(MessageInfo::getMessageKey, messageKey)
                .eq(MessageInfo::getMessageHeader, messageHeader)
                .eq(MessageInfo::getTenantId, tenantId)
                .eq(MessageInfo::getIsDelete, NumberCst.ZERO));
    }

    @Override
    public void deleteMessageByHeader(String messageTopic, String messageHeader, Long tenantId) {
        messageInfoMapper.updateByNoStrategy(Wrappers.lambdaUpdate(MessageInfo.class)
                .set(MessageInfo::getIsDelete, NumberCst.ONE)
                .eq(MessageInfo::getMessageTopic, messageTopic)
                .eq(MessageInfo::getMessageHeader, messageHeader)
                .eq(MessageInfo::getTenantId, tenantId)
                .eq(MessageInfo::getIsDelete, NumberCst.ZERO));
    }
}
